package com.lightboat.web.controller.crm;

import cn.hutool.core.bean.BeanUtil;
import com.lightboat.common.annotation.Anonymous;
import com.lightboat.common.annotation.Log;
import com.lightboat.common.core.controller.BaseController;
import com.lightboat.common.core.domain.AjaxResult;
import com.lightboat.common.core.domain.bo.LogisticsPrintBo;
import com.lightboat.common.enums.BusinessType;
import com.lightboat.system.domain.request.LogisticsPrintRequest;
import com.lightboat.system.service.IJdLogisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物流Controller
 *
 * @author lightboat
 * @date 2025-08-21
 */
@Slf4j
@Tag(name = "物流")
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/logistics")
public class CrmLogisticsController extends BaseController {

    private final IJdLogisticsService jdLogisticsService;

    /**
     * 物流面单打印
     */
    @Operation(summary = "物流面单打印")
    @PostMapping("/print")
    @PreAuthorize("@ss.hasPermi('system:logistics:print')")
    @Log(title = "物流", businessType = BusinessType.EXPORT)
    public AjaxResult print(@RequestBody @Validated LogisticsPrintRequest request) {
        LogisticsPrintBo printBo = BeanUtil.copyProperties(request, LogisticsPrintBo.class);
        printBo.setOperator(getUserId().toString());
        List<String> printUrls = jdLogisticsService.orderPrint(printBo);
        return success(printUrls);
    }

    @Operation(summary = "物流面单模板")
    @GetMapping("/templates")
    public AjaxResult templates() {
        return success(jdLogisticsService.getOrderPrintTemplates());
    }


    @Anonymous
    @PostMapping("/orders/intercept/sub")
    @Operation(summary = "京东物流拦截订阅")
    public AjaxResult jdInterceptSub(@RequestBody String interceptData){

        log.info("京东物流拦截订阅: {}", interceptData);
        jdLogisticsService.interceptSub(interceptData);

        return new AjaxResult(0, "成功");
    }

    @Anonymous
    @PostMapping("/orders/trace_msg/sub")
    @Operation(summary = "京东物流轨迹信息订阅")
    public AjaxResult jdTraceMsgSub(@RequestBody String traceMsg){

        log.info("京东物流轨迹信息订阅: {}", traceMsg);
        jdLogisticsService.jdTraceMsgSub(traceMsg);

        return new AjaxResult(0, "成功");
    }



}
